package com.zhuss.opus.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.SearchKeyWordBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.opus.invit.SearchInvitBo;
import com.zhuss.bean.bo.opus.invit.wechat.ListInvitBo;
import com.zhuss.bean.vo.DetailKeyWordVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.bean.vo.opus.invit.DetailInvitVo;
import com.zhuss.bean.vo.opus.invit.ListInvitVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.opus.db.dao.ext.InvitDao;
import com.zhuss.opus.db.dao.ext.InvitTopicDao;
import com.zhuss.opus.db.entity.Invit;


/**
 * 查询帖子 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("invitRead")
public class InvitRead {
	
	@Autowired
	private InvitDao invitDao;
	
	@Autowired
	private InvitTopicDao invitTopicDao;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	/**
	 * 帖子 
	 * @return
	 */
	public Object listInvitId(Integer area) {
		List<Invit> invitList = invitDao.listAll(area);
		List<Long> result = new ArrayList<>();
		transferListId(invitList, result);
		return result;
	}
	
	
	/**
	 * keyWord
	 * @param bo
	 * @return
	 */
	public PageVo<DetailKeyWordVo> keyWord(SearchKeyWordBo bo) {
		PageVo<DetailKeyWordVo> result = new PageVo<DetailKeyWordVo>();
		List<DetailKeyWordVo> list = new ArrayList<DetailKeyWordVo>();
		list = invitDao.keyWord(bo);
		Integer count = invitDao.countKeyWord(bo);
		result.setPageNum(bo.getPageNum());
		result.setPageSize(bo.getPageSize());
		result.setTotal(count);
		result.setItems(list);
		return result;
	}
	
	/**
	 * 根据评论来搜索
	 * keyWord
	 * @param bo
	 * @return
	 */
	public List<Long> keyWordByTopic(SearchKeyWordBo bo) {
		List<Long> result = new ArrayList<>();
		result = invitTopicDao.keyWord(bo);
		return result;
	}
	
	
	/**
	 * 帖子列表
	 * @param bo
	 * @return
	 * @throws WpsException 
	 */
	public Object listInvit(ListInvitBo bo) throws WpsException {
		PageVo<DetailInvitVo> result = new PageVo<DetailInvitVo>();
		List<DetailInvitVo> list = new ArrayList<DetailInvitVo>();
		list = invitDao.listInvit(bo);
		transferList(list);
		Integer count = invitDao.countInvitList(bo);
		result.setPageNum(bo.getPageNum());
		result.setPageSize(bo.getPageSize());
		result.setTotal(count);
		result.setItems(list);
		return result;
	}
	
	private void transferList(List<DetailInvitVo> list) throws WpsException {
		for (DetailInvitVo vo : list) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
		}
		
	}


	private void transferListId(List<Invit> invitList, List<Long> result) {
		for (Invit item : invitList) {
			result.add(item.getInvitId());
		}
	}

	/**
	 * 帖子列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchInvitBo bo) {
		PageVo<ListInvitVo> result = new PageVo<ListInvitVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(invitDao.list(bo));
        result.setTotal(invitDao.countList(bo));
		return result;
	}
	
	/**
	 * 帖子详情
	 * @param invitId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long invitId) throws WpsException {
		Object result = invitDao.detail(invitId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在, "invitId: " + String.valueOf(invitId));
		}
		return result;
	}
	
	

}
